define(function(require, exports, module) {
	'use strict';
    
    var rk = require('rk');
    //
    
    require('core/services/2.0/rk_workreportService');
    require('./subpanel/wrSubPanelCtrl');
    require('./subpanel/wrListCtrl');

    $.widget('rk.workreportPanelCtrl', $.rk.oaPanelCtrl, {
        //create方法中用来生成html
        _create: function () {
            var me = this;
            var elem = me.element;

            //1，模板文件请在WEB-INFO/templates/oa中追加
            //2，请在src/resources/sub-deps/oa中引入
            //3，如下所示，调用rk.template方法生成html
            var html = require('oa/tmpl/workreport/tpl_workreport_panel.tpl');

            //将html显示在elem中
            workreportService.getInitInfo(function (data) {
                    me.initData = data;
                    data.permission.func =(data.permission.func.toLowerCase() === 'true');
                    var tpl_html = rk.templateText(html,data.permission);
                    elem.html(tpl_html);
                    elem.on('click','.oa-close-panel',function(){
                        rk.oa.closePanel();
                    });

            }, function(){
                me._initAccordion();
                me._initCreateMenu();
            });



        },
        //init方法中，用来初始化页面和数据
        _init: function () {
            var me = this;
            var elem = me.element;
        },
        initData: null,
        accordion: null,
        _initAccordion: function () {
            var me = this;
            var elem = me.element;

            var content = me.getPanelContent();
            me.accordion = content.find('.oa-panel-accordion');
            content.find('span.search').beTip({dir:'left'});
            content.find('span.nav-more').beTip({dir:'left'});
            me.accordion.rkAccordion({
                //面板打开
                onOpen: function (e, ui/*index, panel*/) {
                    me._onAccordionOpen(e, ui);
                },
                onTitleBarClick: function (e) {
                    var target = $(e.target);
                    if(target.parents('.oa-head-toolbar').size() == 0){//点在了非toolbar区域
                        rk.oa.closeContent();
                    }
                },
                beforeActivate: function(event, ui ){
                    var panel = ui.newPanel;
                    if(panel.data('wrSubPanelCtrl')){
                        panel.wrSubPanelCtrl('destroy');
                        panel.data('wrSubPanelCtrl', null);
                    }
                    panel.beLoading();
                }
            });

        },

        //------------------------------------------------------------------------------------------
        _onAccordionOpen: function (e, ui/*index, panel*/) {
            var me = this;
            var elem = me.element;
            var panel = ui.panel;
            //初始化面板
            me._createSubPanel(me.initData, panel, ui.index);
            //工具条

            var titleBar = me.accordion.rkAccordion('getTitleBar', ui.index);
            titleBar.find('.oa-head-toolbar').css('visibility', '');


        },


        _createSubPanel: function (initData, panel, panelIndex) {
            var me = this;
            var elem = me.element;
            var titleBar = panel.prev();
            var act = titleBar.attr('act');

            if (panelIndex === 0) {
                panel.wrSubPanelCtrl({
                    businessTypes: initData.businessTypes,
                    results: initData.results,
                    dataUrl: 'workreport.mine',
                    templateId: 'oa/tmpl/workreport/asset/template_my_row.tpl'
                });
            } else if (panelIndex === 1) {
                panel.wrSubPanelCtrl({
                    businessTypes: initData.businessTypes,
                    dataUrl: 'workreport.received',
                    templateId: 'oa/tmpl/workreport/asset/template_collage_row.tpl'
                });
            } else if (panelIndex === 2) {
                panel.wrSubPanelCtrl({
                    businessTypes: initData.businessTypes,
                    dataUrl: 'workreport.subordinate',
                    templateId: 'oa/tmpl/workreport/asset/template_collage_row.tpl'
                });
            }
        },
        _initCreateMenu: function () {
            var me = this;
            var elem = me.element;
            var header = me.getPanelHeader();
            //新建菜单

            header.find('#wr-createBtn').rkEasyMenu({
                menuId: "wr-createMenu",
                onSelect: function (li) {
                    var act = li.attr('act');
                    var belongTypeId;
                    var info = workreportService.getBelongTypeInfoByType(act);
                    belongTypeId = info.id;

                    workreportService.popupEditor({
                        editorAct: 'create',
                        editorType: act,
                        belongTypeId: belongTypeId
                    });
                }
            });
        }
    });
});